package com.weipai.demo.controller;

import com.weipai.demo.enums.RetCode;
import com.weipai.demo.model.House;
import com.weipai.demo.model.HouseType;
import com.weipai.demo.service.FileUploadService;
import com.weipai.demo.service.HouseService;
import com.weipai.demo.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 房贷
 */
@RestController
public class HouseController {
    @Autowired
    private HouseService houseService;
    @Autowired
    private FileUploadService fileUploadService;

    @RequestMapping(value = "/house/getList")
    public Response getList(Integer page,Integer size,House house,Integer front,Integer sort,String sortType){
        return houseService.getList(page,size,house,front,sort,sortType);
    }

    @RequestMapping(value = "/house/addHouse")
    public Response saveHouse(House house,MultipartFile logo, HttpServletRequest request){
        Response response = new Response();
        if (logo != null) {
            String result = fileUploadService.saveImgUpload(request, logo);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
                return response;
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
                return response;
            } else if (result.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
                return response;
            } else {
                house.setLogoImg(result);
            }
        }else {
            house.setLogoImg(request.getParameter("img"));
        }
        return houseService.addHouse(house);
    }

    @RequestMapping(value = "/house/delete")
    public Response delHouse(Integer id){
        return houseService.delHouse(id);
    }
    @RequestMapping(value = "/houseType/getList")
    public Response getHouseTypeList(Integer page,Integer size,Integer id){
        return houseService.getHouseTypeList(page,size,id);
    }

    @RequestMapping(value = "/houseType/save")
    public Response saveHouseType(HouseType houseType){
        return houseService.addHouseType(houseType);
    }

    @RequestMapping(value = "/houseType/delete")
    public Response delHouseType(Integer id){
        return houseService.delHouseType(id);
    }

    //上下架修改
    @RequestMapping(value = "/house/changePublishStatus")
    public Response changePublishStatus(Integer id,Integer publishStatus){
        return houseService.changePublishStatus(id,publishStatus);
    }
    //房贷详情
    @RequestMapping(value = "/house/getDetail")
    public Response getDetail(Integer id){
        return houseService.getDetail(id);
    }

}
